<?php
namespace app\api\controller\quot;
use think\File;
use think\facade\Env;
use app\common\model\user\User as UserModel;
use app\common\model\company\Company;
use app\common\model\orderdata\Goods;
use app\common\model\quotation\QuotationList;
use app\api\controller\Base;
use app\common\model\power\Rolepower;
use app\api\controller\mytool\Sendmessage;
class Manaquot extends Base
{
	/**
	 * 产生jwt加密串，参考session
	 */
    public function initialize(){
		parent::initialize();
		self::is_login();
		Rolepower::powerguard(203002);
	}

	
	/**
	 * 修改/完善企业信息 
	 * 产废人信息也再次完善
	 * @return [type] [description]
	 */
	public function create(){
		$data = input('post.');
		$data['GoodId'] = explode(',',$data['GoodId']);
		$data['value'] = explode(',',$data['value']);
		//列表判断
		$validate =  $this->validate($data,'app\common\validate\Quotation');
		if (true !== $validate) {
		    return myerr($validate);
		}
		if($data['StartTime'] < date('Y-m-d H:i',time())){
			$data['StartTime'] = date('Y-m-d H:i',time());
			// return myerr('生效时间已过，请修改。');
		}
		//特殊产品判断
		if(!empty($data['GoodId'])){
			$sb = Goods::list_state($data['GoodId']);
			if(!$sb['code']){
				return myerr($sb['msg']);
			}

			if(count($data['GoodId'])!==count($data['value'])){
				return myerr('危废种类与价格不匹配，请重新尝试');
			}
		}
		//创建报价表
		$qu = new QuotationList();
		$data = $qu->screate($data);
		$type = input('Type');
        if(strstr($type,'hawa')!==false){
           $sb = Sendmessage::Quotation(1,$data); 
        }else if(strstr($type,'buss')!==false){
           $sb = Sendmessage::Quotation(2,$data);
        }
        if(!$sb['code']){
			return myok('报价已创建，'.$sb['msg']);
		}
		return myok('报价信息已确认');
	}

	/**
	 * 获取列表
	 * @return [type] [description]
	 */
	public function list(){
		$data = input('get.');
		$data['Type'] = 'quot';
		$data = init_where($data);
		$qu = new QuotationList();
		$list = $qu->usergetlist($data);
		foreach ($list['list'] as $key => $value) {
			if(empty($value['setuserinfo']['Name'])){
				$list['list'][$key]['Name'] = '暂未完善名称';
			}else{
				$list['list'][$key]['Name'] = $value['setuserinfo']['Name'];
			}
		}
		$list['tr']['title']  = ['生效时间','结束时间','报价状态','查看','设置时间','设置人','通用价格','服务费'];
		$list['tr']['key'] = ['StartTime','EndTime','State','QuotationListId','SetTime','Name','Price','CarPrice'];
		$list['tr']['show'] = [true,true,true,true,true,true,false,false];
		return myok($list);
	}

	/**
	 * 获取订单信息
	 * @return [type] [description]
	 */
	public function getinfo(){
		$id = input('id');
		if(empty($id)){
			return myerr('缺少参数，请重新尝试');
		}
		$qu = new QuotationList();
		$data =	$qu->getinfo($id,'mana');
		if(!$data['code']){
			return myerr($data['msg']);
		}
		return myok($data);
	}

	/**
	 * 图片上传
	 * @return [type] [description]
	 */
	public function upimg(){
		$type = empty(input('type'))?'other':input('type');
		if(!$type=='company'||!$type=='user'||!$type=='order'){
			$type = 'other';
		}
		try {
			$file = request()->file('img');
		} catch (\Exception $e) {
			return json_encode(['status'=>'error','data'=>$e->getMessage()]);
		}
        // 移动到框架应用根目录/public/uploads/ 目录下
        if(!is_null($file)){
           	try{
           		$save_path = Env::get('root_path').'public\\static' .'\\upimg\\hawa\\'.$type.'\\';
	            $info = $file->validate(["size"=>50000000,'ext'=>'jpg,png,gif'])->rule("date")->move($save_path);
	            //echo $save_path. DS . date("Ymd") . DS .  $info->getFilename();
	            $image = \think\Image::open($save_path . '\\' . $info->getSaveName());  
	            $image->thumb(2000, 2000)->save($save_path .  '\\' . $info->getSaveName());  
	            if($info){//getSaveName();
	                $p=$info->getFilename();
	                return json_encode(['status'=>'ok','data'=>SERVERURL.'static/upimg/hawa/'.$type.'/'.date('Ymd',time()).'/'.$p]);
	            }else{
	              return json_encode(['status'=>'error','data'=>$info->getError()]);
	            }
           	} catch (\Exception $e) {
				return json_encode(['status'=>'error','data'=>$e->getMessage()]);
			}	
            
         }else{
              return json_encode(['status'=>'error','data'=>'没有找到需要上传的图片']);
         }
	}


	/**
	 * 判断账号是否登录
	 * @return boolean [description]
	 */
	private function is_login(){
		$data = UserModel::is_sessionlogin();
		if (!$data['code']) {
			$data['options'] = 'relogin';
			json($data)->send();
			exit;
		}
	}
}
